/*
 * CadastroUsuario.java
 *
 * Created on 20/10/2011, 23:34:31
 */
package org.acervus.gui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import org.acervus.usuario.UsuarioBean;
import org.acervus.usuario.UsuarioController;
import org.acervus.helpers.DateUtils;

/**
 * @author Eziel Soares, Giordano Giuliano, Lenilson Regis
 */
public class CadastroUsuario extends javax.swing.JFrame {

    DefaultTableModel tmCadastroUsuario = new DefaultTableModel(null, new String[]{"Login", "Nome", "Email", "Telefone"});
    ListSelectionModel lsmCadastroUsuario;
    List<UsuarioBean> usuarios;
    String tipoCadastro;

    /** Creates new form CadastroUsuario */
    public CadastroUsuario() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfPesquisarUsuario = new javax.swing.JTextField();
        btPesquisaUsuario = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbCadastroUsuario = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        tfLogin = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        tfEmail = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfEndereco = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfDataNasc = new javax.swing.JTextField();
        try{
            javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("##/##/####");
            tfDataNasc = new javax.swing.JFormattedTextField(data);
        }
        catch (Exception e){
        }
        jLabel8 = new javax.swing.JLabel();
        tfCidade = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        tfBairro = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        tfUf = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        tfTelefone = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        tfCelular = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        tfTelRecado = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        tfNomeRecado = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        btNovo = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        tfDataCadastro = new javax.swing.JTextField();
        DateUtils dataAtual = new DateUtils(new Date());
        tfDataCadastro.setText(dataAtual.getDataAtual());
        btCancelar = new javax.swing.JButton();
        pfSenha = new javax.swing.JPasswordField();
        btFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Usuários");
        setAlwaysOnTop(true);

        jLabel1.setText("Usuário:");

        btPesquisaUsuario.setText("Pesquisar");
        btPesquisaUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisaUsuarioActionPerformed(evt);
            }
        });

        tbCadastroUsuario.setModel(tmCadastroUsuario);
        tbCadastroUsuario.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsmCadastroUsuario = tbCadastroUsuario.getSelectionModel();
        lsmCadastroUsuario.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e) {
                if (! e.getValueIsAdjusting()){
                    tbCadastroUsuarioLinhaSelecionada(tbCadastroUsuario);
                    btAlterar.setEnabled(true);
                }

            }
        });
        listarUsuario();
        jScrollPane1.setViewportView(tbCadastroUsuario);

        jLabel2.setText("Login:");

        tfLogin.setEnabled(false);

        jLabel3.setText("Senha:");

        jLabel4.setText("Email:");

        jLabel5.setText("Nome:");

        tfNome.setEnabled(false);

        tfEmail.setEnabled(false);

        jLabel6.setText("Endereço:");

        tfEndereco.setEnabled(false);

        jLabel7.setText("Data de Nascimento:");

        tfDataNasc.setEnabled(false);

        jLabel8.setText("UF:");

        tfCidade.setEnabled(false);

        jLabel9.setText("Bairro:");

        tfBairro.setEnabled(false);

        jLabel10.setText("Cidade:");

        tfUf.setEnabled(false);

        jLabel11.setText("Telefone:");

        tfTelefone.setEnabled(false);

        jLabel12.setText("Celular:");

        tfCelular.setEnabled(false);

        jLabel13.setText("Tel. p/ Recado:");

        tfTelRecado.setEnabled(false);

        jLabel14.setText("Nome p/ Recado:");

        tfNomeRecado.setEnabled(false);

        jLabel15.setText("Data de Cadastro:");

        btNovo.setText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        btAlterar.setText("Alterar");
        btAlterar.setEnabled(false);
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });

        btSalvar.setText("Salvar");
        btSalvar.setEnabled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        tfDataCadastro.setEnabled(false);

        btCancelar.setText("Cancelar");
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        pfSenha.setText("jPasswordField1");
        pfSenha.setEnabled(false);

        btFechar.setText("Fechar");
        btFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfNome, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(tfLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2)
                                                .addGap(144, 144, 144)))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3)
                                            .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jLabel14)
                                            .addComponent(tfNomeRecado, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                                        .addGap(4, 4, 4))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel7)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(67, 67, 67)
                                            .addComponent(btPesquisaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(tfDataNasc, javax.swing.GroupLayout.Alignment.TRAILING))
                                    .addComponent(tfEmail, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfPesquisarUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(tfDataCadastro)
                        .addComponent(jLabel15, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tfCelular, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                            .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel13)
                            .addComponent(tfTelRecado, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(195, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addContainerGap(416, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(tfBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(tfUf, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel11)
                            .addComponent(tfTelefone, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(16, 16, 16))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(btAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                        .addComponent(btSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfPesquisarUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisaUsuario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel5))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfDataNasc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel8)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfUf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfTelRecado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfNomeRecado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel15)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfDataCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNovo)
                    .addComponent(btFechar)
                    .addComponent(btSalvar)
                    .addComponent(btAlterar)
                    .addComponent(btCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void alterarUsuario() {
        if (verificarCampos()) {
            UsuarioBean usuario = new UsuarioBean();
            usuario.setId(usuarios.get(tbCadastroUsuario.getSelectedRow()).getId());
            usuario.setLogin(tfLogin.getText().trim());
            usuario.setSenha(pfSenha.getPassword().toString());
            usuario.setEmail(tfEmail.getText().trim());
            usuario.setNome(tfNome.getText().trim());
            usuario.setDataNasc(tfDataNasc.getText().trim());
            usuario.setEndereco(tfEndereco.getText().trim());
            usuario.setBairro(tfBairro.getText().trim());
            usuario.setCidade(tfCidade.getText().trim());
            usuario.setUf(tfUf.getText().trim());
            usuario.setTelefone(tfTelefone.getText().trim());
            usuario.setCelular(tfCelular.getText().trim());
            usuario.setTelRecado(tfTelRecado.getText().trim());
            usuario.setNomeRecado(tfNomeRecado.getText().trim());
            usuario.setDataCadastro(tfDataCadastro.getText().trim());
            UsuarioController u = new UsuarioController();
            u.alterarUsuario(usuario);
            desabilitarCampos();
            JOptionPane.showMessageDialog(this, "Dados Alterados!");
        } else {
            JOptionPane.showMessageDialog(this, "Informe o nome do usuário!");
            tfNome.requestFocus();
        }

    }

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if (tipoCadastro.equals("novo")) {
            cadastrarUsuario();
        } else if (tipoCadastro.equals("alteração")) {
            alterarUsuario();
        }

    }//GEN-LAST:event_btSalvarActionPerformed

    private void listarUsuario() {
        UsuarioController usu = new UsuarioController();
        usuarios = usu.listarUsuario("%" + tfPesquisarUsuario.getText().trim() + "%");
        mostrarUsuario(usuarios);
    }

    private void mostrarUsuario(List<UsuarioBean> usuario) {

        while (tmCadastroUsuario.getRowCount() > 0) {
            tmCadastroUsuario.removeRow(0);
        }

        if (usuario.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Nenhum usuário encontrado!");
        } else {
            String[] campos = new String[]{null, null, null, null};
            for (int i = 0; i < usuario.size(); i++) {
                tmCadastroUsuario.addRow(campos);
                tmCadastroUsuario.setValueAt(usuario.get(i).getLogin(), i, 0);
                tmCadastroUsuario.setValueAt(usuario.get(i).getNome(), i, 1);
                tmCadastroUsuario.setValueAt(usuario.get(i).getEmail(), i, 2);
                tmCadastroUsuario.setValueAt(usuario.get(i).getTelefone(), i, 3);
            }
        }
    }

private void btPesquisaUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisaUsuarioActionPerformed
    listarUsuario();
}//GEN-LAST:event_btPesquisaUsuarioActionPerformed

    private void novoUsuario() {
        Date data;
        String dataAtual;
        
        habilitarCampos();
        tfLogin.setText("");
        pfSenha.setText("");
        tfEmail.setText("");
        tfNome.setText("");
        tfDataNasc.setText("");
        tfEndereco.setText("");
        tfBairro.setText("");
        tfCidade.setText("");
        tfUf.setText("");
        tfTelefone.setText("");
        tfCelular.setText("");
        tfTelRecado.setText("");
        tfNomeRecado.setText("");
        
        data = new Date();  
        dataAtual = new SimpleDateFormat("dd/MM/yyyy").format(data);
        tfDataCadastro.setText(dataAtual);
    }

    private void cadastrarUsuario() {
        if (verificarCampos()) {
            UsuarioBean usuario = new UsuarioBean();
            usuario.setLogin(tfLogin.getText().trim());
            usuario.setSenha(pfSenha.getPassword().toString());
            usuario.setEmail(tfEmail.getText().trim());
            usuario.setNome(tfNome.getText().trim());

            DateUtils data_nasc = new DateUtils(tfDataNasc.getText().trim());
            usuario.setDataNasc(data_nasc.getDataISO());

            usuario.setEndereco(tfEndereco.getText().trim());
            usuario.setBairro(tfBairro.getText().trim());
            usuario.setCidade(tfCidade.getText().trim());
            usuario.setUf(tfUf.getText().trim());
            usuario.setTelefone(tfTelefone.getText().trim());
            usuario.setCelular(tfCelular.getText().trim());
            usuario.setTelRecado(tfTelRecado.getText().trim());
            usuario.setNomeRecado(tfNomeRecado.getText().trim());

            DateUtils data_cad = new DateUtils(tfDataCadastro.getText().trim());
            usuario.setDataCadastro(data_cad.getDataISO());

            UsuarioController u = new UsuarioController();
            u.cadastrarUsuario(usuario);
            desabilitarCampos();
            JOptionPane.showMessageDialog(this, "Usuário cadastrado com sucesso!");
            listarUsuario();
        }

    }

    private boolean verificarCampos() {
        // Verifica se há dados no campo: login
        if (!tfLogin.getText().trim().equals("")) {
            return true;
        } else {
            JOptionPane.showMessageDialog(this, "O campo LOGIN está vazio!");
        }

        // Verifica se há dados no campo: senha
        if (!pfSenha.getText().trim().equals("")) {
            return true;
        } else {
            JOptionPane.showMessageDialog(this, "O campo SENHA está vazio!");
        }

        // Verifica se há dados no campo: nome
        if (!tfNome.getText().trim().equals("")) {
            return true;
        } else {
            JOptionPane.showMessageDialog(this, "O campo NOME está vazio!");
        }

        // Verifica se há dados no campo: email
        if (!tfEmail.getText().trim().equals("")) {
            return true;
        } else {
            JOptionPane.showMessageDialog(this, "O campo EMAIL está vazio!");
        }

        // Verifica se há dados no campo: telefone
        if (!tfEmail.getText().trim().equals("")) {
            return true;
        } else {
            JOptionPane.showMessageDialog(this, "O campo TELEFONE está vazio! Pelo menos um telefone deve ser informado.");
        }
        return false;
    }

    private void habilitarCampos() {
        tfLogin.setEnabled(true);
        pfSenha.setEnabled(true);
        tfEmail.setEnabled(true);
        tfNome.setEnabled(true);
        tfDataNasc.setEnabled(true);
        tfEndereco.setEnabled(true);
        tfBairro.setEnabled(true);
        tfCidade.setEnabled(true);
        tfUf.setEnabled(true);
        tfTelefone.setEnabled(true);
        tfCelular.setEnabled(true);
        tfTelRecado.setEnabled(true);
        tfNomeRecado.setEnabled(true);
        // tfDataCadastro.setEnabled(true);
        btSalvar.setEnabled(true);
        btCancelar.setEnabled(true);
    }

    private void desabilitarCampos() {
        tfLogin.setEnabled(false);
        pfSenha.setEnabled(false);
        tfEmail.setEnabled(false);
        tfNome.setEnabled(false);
        tfDataNasc.setEnabled(false);
        tfEndereco.setEnabled(false);
        tfBairro.setEnabled(false);
        tfCidade.setEnabled(false);
        tfUf.setEnabled(false);
        tfTelefone.setEnabled(false);
        tfCelular.setEnabled(false);
        tfTelRecado.setEnabled(false);
        tfNomeRecado.setEnabled(false);
        // tfDataCadastro.setEnabled(false);
        btSalvar.setEnabled(false);
        btCancelar.setEnabled(false);
    }

private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
    tipoCadastro = "novo";
    novoUsuario();
}//GEN-LAST:event_btNovoActionPerformed

    private void alteraUsuario() {
        if (tbCadastroUsuario.getSelectedRow() != -1) {
            habilitarCampos();
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um usuário!");

        }

    }

private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
    tipoCadastro = "alteração";
    alteraUsuario();
}//GEN-LAST:event_btAlterarActionPerformed

private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
    desabilitarCampos();
}//GEN-LAST:event_btCancelarActionPerformed

private void btFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFecharActionPerformed
    desabilitarCampos();
    this.dispose();
}//GEN-LAST:event_btFecharActionPerformed

    private void tbCadastroUsuarioLinhaSelecionada(JTable tb) {
        if (tb.getSelectedRow() != -1) {
            tfLogin.setText(usuarios.get(tb.getSelectedRow()).getLogin());
            pfSenha.setText(usuarios.get(tb.getSelectedRow()).getSenha());
            tfNome.setText(usuarios.get(tb.getSelectedRow()).getNome());
            tfEmail.setText(usuarios.get(tb.getSelectedRow()).getEmail());

            //DateUtils data_nasc = new DateUtils();
            tfDataNasc.setText(usuarios.get(tb.getSelectedRow()).getDataNasc());

            tfEndereco.setText(usuarios.get(tb.getSelectedRow()).getEndereco());
            tfBairro.setText(usuarios.get(tb.getSelectedRow()).getBairro());
            tfCidade.setText(usuarios.get(tb.getSelectedRow()).getCidade());
            tfUf.setText(usuarios.get(tb.getSelectedRow()).getUf());
            tfTelefone.setText(usuarios.get(tb.getSelectedRow()).getTelefone());
            tfCelular.setText(usuarios.get(tb.getSelectedRow()).getCelular());
            tfTelRecado.setText(usuarios.get(tb.getSelectedRow()).getTelRecado());
            tfNomeRecado.setText(usuarios.get(tb.getSelectedRow()).getNomeRecado());
            tfDataCadastro.setText(usuarios.get(tb.getSelectedRow()).getDataCadastro());
        } else {
            tfLogin.setText("");
            pfSenha.setText("");
            tfEmail.setText("");
            tfNome.setText("");
            tfDataNasc.setText("");
            tfEndereco.setText("");
            tfBairro.setText("");
            tfCidade.setText("");
            tfUf.setText("");
            tfTelefone.setText("");
            tfCelular.setText("");
            tfTelRecado.setText("");
            tfNomeRecado.setText("");
            tfDataCadastro.setText("");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastroUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new CadastroUsuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btFechar;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisaUsuario;
    private javax.swing.JButton btSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPasswordField pfSenha;
    private javax.swing.JTable tbCadastroUsuario;
    private javax.swing.JTextField tfBairro;
    private javax.swing.JTextField tfCelular;
    private javax.swing.JTextField tfCidade;
    private javax.swing.JTextField tfDataCadastro;
    private javax.swing.JTextField tfDataNasc;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfEndereco;
    private javax.swing.JTextField tfLogin;
    private javax.swing.JTextField tfNome;
    private javax.swing.JTextField tfNomeRecado;
    private javax.swing.JTextField tfPesquisarUsuario;
    private javax.swing.JTextField tfTelRecado;
    private javax.swing.JTextField tfTelefone;
    private javax.swing.JTextField tfUf;
    // End of variables declaration//GEN-END:variables
}
